namespace SinaFinance.ChartLibrary.AxisLibrary
{
    using SinaFinance;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;

    public partial class ComparationArea : UserControl
    {
        //private bool _contentLoaded;
        public string ComparedStockID = string.Empty;
        private CompareEventHanlder doCompare;
        private DoQueryEventHanlder doQuery;
        private System.EventHandler doSearch;
        private bool isReviewAreaHidden = true;
        public List<ComparationItem> items = new List<ComparationItem>();
        //internal Grid LayoutRoot;
        public string MainStockID = string.Empty;
        //internal Canvas rootCanvas;
        private ShowOrHiddenReviewAreaHandler showOrHiddenReview;
        private const double spanLen = 10.0;

        public event CompareEventHanlder DoCompare
        {
            [MethodImpl(MethodImplOptions.Synchronized)] add
            {
                this.doCompare = (CompareEventHanlder)Delegate.Combine((Delegate)this.doCompare, (Delegate)value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)] remove
            {
                this.doCompare = (CompareEventHanlder)Delegate.Remove((Delegate)this.doCompare, (Delegate)value);
            }
        }

        public event DoQueryEventHanlder DoQuery
        {
            [MethodImpl(MethodImplOptions.Synchronized)] add
            {
                this.doQuery = (DoQueryEventHanlder)Delegate.Combine((Delegate)this.doQuery, (Delegate)value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)] remove
            {
                this.doQuery = (DoQueryEventHanlder)Delegate.Remove((Delegate)this.doQuery, (Delegate)value);
            }
        }

        public event System.EventHandler DoSearch
        {
            [MethodImpl(MethodImplOptions.Synchronized)] add
            {
                this.doSearch = (System.EventHandler)Delegate.Combine((Delegate)this.doSearch, (Delegate)value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)] remove
            {
                this.doSearch = (System.EventHandler)Delegate.Remove((Delegate)this.doSearch, (Delegate)value);
            }
        }

        public event ShowOrHiddenReviewAreaHandler ShowOrHiddenReview
        {
            [MethodImpl(MethodImplOptions.Synchronized)] add
            {
                this.showOrHiddenReview = (ShowOrHiddenReviewAreaHandler)Delegate.Combine((Delegate)this.showOrHiddenReview, (Delegate)value);
            }
            [MethodImpl(MethodImplOptions.Synchronized)] remove
            {
                this.showOrHiddenReview = (ShowOrHiddenReviewAreaHandler)Delegate.Remove((Delegate)this.showOrHiddenReview, (Delegate)value);
            }
        }

        public ComparationArea()
        {
            this.InitializeComponent();
            base.Loaded += new RoutedEventHandler(this.ComparationArea_Loaded);
        }

        private void ComparationArea_Loaded(object sender, RoutedEventArgs e)
        {
            string cookie = CookieControl.GetCookie("cookie_history");
            if (!string.IsNullOrEmpty(cookie))
            {
                List<string> list = cookie.Split(new char[] { '$' }).ToList<string>();
                if (!list.Count.Equals(0))
                {
                    TextBlock block = new TextBlock {
                        Foreground = new SolidColorBrush(Colors.White),
                        Text = "浏览历史",
                        Cursor = Cursors.Hand,
                        Margin = new Thickness(10.0, 0.0, 0.0, 0.0)
                    };
                    block.MouseLeftButtonDown += new MouseButtonEventHandler(this.tbl_MouseLeftButtonDown);
                    this.rootCanvas.Children.Add(block);
                    int num = 0;
                    for (int i = list.Count - 1; i > -1; i--)
                    {
                        string[] strArray = list[i].Split(new char[] { '_' });
                        int length = strArray.Length;
                        if (length.Equals(3))
                        {
                            ComparationItem item = new ComparationItem {
                                Width = 115.0,
                                ParentControl = this,
                                ItemText = strArray[1] + " " + strArray[2] + " ",
                                Tag = strArray[0] + "^" + strArray[1] + "^" + strArray[2]
                            };
                            this.rootCanvas.Children.Add(item);
                            item.Margin = new Thickness(((10.0 + block.ActualWidth) + 10.0) + ((item.Width + 10.0) * num), 0.0, 0.0, 0.0);
                            item.Cursor = Cursors.Hand;
                            item.MouseLeftButtonDown += new MouseButtonEventHandler(this.item_MouseLeftButtonDown);
                            item.DoCompare += new ComparationItem.CompareEventHanlder(this.item_DoCompare);
                            item.DoQuery += new ComparationItem.SearchHanlder(this.item_DoQuery);
                            this.items.Add(item);
                            if (!(this.MainStockID.Equals(string.Empty) || !strArray[1].Equals(this.MainStockID)))
                            {
                                item.IsItemEnable = false;
                                item.IsItemChecked = true;
                            }
                            if (!(this.ComparedStockID.Equals(string.Empty) || !strArray[1].Equals(this.ComparedStockID)))
                            {
                                item.IsItemChecked = true;
                            }
                            num++;
                        }
                    }
                }
            }
        }

        //[DebuggerNonUserCode]
        //public void InitializeComponent()
        //{
        //    if (!this._contentLoaded)
        //    {
        //        this._contentLoaded = true;
        //        Application.LoadComponent(this, new Uri("/SinaFinance;component/ChartLibrary/AxisLibrary/ComparationArea.xaml", UriKind.Relative));
        //        this.LayoutRoot = (Grid) base.FindName("LayoutRoot");
        //        this.rootCanvas = (Canvas) base.FindName("rootCanvas");
        //    }
        //}

        private void item_DoCompare(object sender, RoutedEventArgs e)
        {
            if (this.doCompare != null)
            {
                this.doCompare(sender, e);
            }
        }

        private void item_DoQuery(object sender, RoutedEventArgs e)
        {
            if (this.doQuery != null)
            {
                this.doQuery(sender, e);
            }
        }

        private void item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.doSearch != null)
            {
                this.doSearch(sender, e);
            }
        }

        public void SetItemsUnchecked()
        {
            for (int i = 0; i < this.items.Count; i++)
            {
                this.items[i].ClearCheckStatus();
            }
        }

        private void tbl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.showOrHiddenReview != null)
            {
                this.showOrHiddenReview(sender, e);
            }
        }

        public delegate void CompareEventHanlder(object sender, RoutedEventArgs e);

        public delegate void DoQueryEventHanlder(object sender, RoutedEventArgs e);

        public delegate void EventHanlder(object sender, MouseEventArgs e);

        public delegate void ShowOrHiddenReviewAreaHandler(object sender, MouseEventArgs e);
    }
}
